package org.proyectosinapsis.Sinapsis;

/**
 * User: Alexander Simoes
 * Company: CoralVision Systems
 * Date: 9/12/12
 */

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
//ToDo Check that the sex registered is correct
public class RegisterActivity extends Activity {

	//Function that checks if all EditText in the layout are not empty
	//It displays a floating text asking the user for the input
	//True if it finds at least one that is empty
    private boolean checkForm() {

	    ViewGroup group = (ViewGroup) findViewById(R.id.registerForm);
	    String errorMsg = getString(R.string.emptyForm);
	    int strLenght = errorMsg.length();

	    for (int i = 0, count = group.getChildCount(); i < count; ++i) {
		    View view = group.getChildAt(i);
		    if (view instanceof EditText) {
			    String str = ((EditText)view).getText().toString();
			    if (str.trim().equals("")) {
				    errorMsg = errorMsg + "\n" + ((EditText)view).getHint().toString();
			    }
		    }
	    }

	    if (errorMsg.length()!=strLenght) {
		    Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
		    return true;
	    }

	    return false;
    }

	//Function that checks that email have correct format
	//True if there's an error
	private boolean checkEmail() {
		String email = ((EditText)findViewById(R.id.reg_email)).getText().toString();
		if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
			Toast.makeText(this, getString(R.string.wrongEmailFormat), Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}

	//Function that checks that passwords match
	//True if there's an error
	private boolean checkPasswords() {
		String pass1 = ((EditText)findViewById(R.id.reg_password)).getText().toString();
		String pass2 = ((EditText)findViewById(R.id.reg_password2)).getText().toString();
		if (!pass1.equals(pass2)) {
			Toast.makeText(this, getString(R.string.passUnmatched), Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}

    @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set View to register.xml
		setContentView(R.layout.register);

		ImageView loginScreen = (ImageView) findViewById(R.id.back);
	    Button button = (Button) findViewById(R.id.btnRegister);

		// Listening to Back Logo
		loginScreen.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				// Closing registration screen
				// Switching to Login Screen/closing register screen
				finish();
			}
		});

	    //Listening to register button
	    button.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View arg0) {
			    if (checkForm() || checkPasswords() || checkEmail()) {
				    return;
			    }
			    // Closing registration screen
			    // Switching to Login Screen/closing register screen
			    finish();
		    }
        });
	}
}

// ToDo: Check that ID Pacient is correct
// ToDo: Check that username is unique
// ToDo: Register to DB